
package xj.graph2d.template;

import xj.graph2d.Document;
import xj.graph2d.GraphEdgeBase;
import xj.graph2d.GraphNodeBase;
import xj.meta.ModelElement;
import xj.model.uml.UMLDiagram;

public class UMLDiagramConnectionChecker { 

  // check if an edge can be connected to a node

  static public boolean checkConnectSource(Document.DocumentType doctype, 
					   ModelElement graphModel, 
					   GraphEdgeBase edge, GraphNodeBase node) { 
    //System.out.println("UMLDiagramConnectionChecker.checkConnectSource()");
    if (doctype != null && 
	graphModel != null && 
	edge != null && 
	node != null) {
      return checkConnect(doctype, graphModel, edge, node, true);
    }
    return true;
  }

  static public boolean checkConnectDestination(Document.DocumentType doctype, 
						ModelElement graphModel, 
						GraphEdgeBase edge, GraphNodeBase node) { 
    //System.out.println("UMLDiagramConnectionChecker.checkConnectDestination()");
    if (doctype != null && 
	graphModel != null && 
	edge != null && 
	node != null) {
      return checkConnect(doctype, graphModel, edge, node, false);
    }
    return true;
  }


  static protected boolean checkConnect(Document.DocumentType doctype, 
					ModelElement graphModel, 
					GraphEdgeBase edge, 
					GraphNodeBase node, 
					boolean source) { 
    //System.out.println("UMLDiagramConnectionChecker.checkConnect() " + source);
    if (graphModel != null && 
	edge != null && 
	node != null) {
      ModelElement emodel = edge.getModel();
      ModelElement nmodel = node.getModel();
      if (graphModel instanceof UMLDiagram) { 	
	UMLDiagram diagram = (UMLDiagram) graphModel; 
	return diagram.checkConnect(emodel, nmodel, source);
      }
      /* 
      switch (doctype) {
      case UML_Package_Diagram:
	
	break; 
	  
      case UML_Class_Diagram:
	
	break;
      case UML_Use_Case_Diagram:
	
	break;
      case UML_State_Diagram:
	
	break;
      case UML_Sequence_Diagram:
	  
	break;
      case UML_Activity_Diagram:
	  
	break;
      }
      */
    }
    return true;    
  }
  
  static public boolean checkAcceptSubnode(Document.DocumentType doctype, 
					   ModelElement graphModel, 
					   ModelElement parentModel, 
					   ModelElement subnodeModel) {
    if (graphModel instanceof UMLDiagram &&
	parentModel != null && 
	subnodeModel != null) {
      UMLDiagram diagram = (UMLDiagram) graphModel; 
      return diagram.checkAllowSubnode(parentModel, subnodeModel);
    }
    return false; 
  }

}
